package esercitazione1;

public class MyStack<T> implements Stack<T> {
	private int count;
	private Node<T> top;

	public MyStack(){
		count = 0;
		top = null;
	}
	
	@Override
	public int size() {
		return count;
	}

	@Override
	public T pop() {
		Node<T> output = top;
		top = top.getNext();
		count--;
		return output.getData();
	}

	@Override
	public T top() {
		return top.getData();
	}

	@Override
	public void push(T input) {
		Node<T> newTop = new Node<T>(input, null);
		newTop.setNext(top);
		top = newTop;
		count++;
	}

	@Override
	public boolean isEmpty() {
		if(count == 0)
			return true;
		return false;
	}

	public String toString(){
		String output = "";
		Node<T> curr = top;
		for(int i = 0; i < count; i++){
			output += curr.getData().toString();
			curr = curr.getNext();
			if(i != count - 1)
				output += "; ";
		}
		return output;
	}
}
